package com.zhuanzhuan.hero.user.facade;

import com.bj58.spat.scf.server.contract.annotation.ServiceBehavior;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ServiceHeaderEntity;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ZZOpenScfBaseResult;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZHeader;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZMethod;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZParam;
import com.bj58.zhuanzhuan.zzscf.common.util.CollectionUtils;
import com.zhuanzhuan.hero.user.consts.NumConsts;
import com.zhuanzhuan.hero.user.pojo.bo.AppInitConfAndroidBO;
import com.zhuanzhuan.hero.user.pojo.bo.AppInitConfigBO;
import com.zhuanzhuan.hero.user.pojo.bo.AppUpgradesVersionBO;
import com.zhuanzhuan.hero.user.service.IApolloService;
import com.zhuanzhuan.hero.user.util.ServiceHeaderUtil;
import com.zhuanzhuan.hero.user.vo.AppInitConfAndroidVO;
import com.zhuanzhuan.hero.user.vo.AppInitConfVO;
import com.zhuanzhuan.hero.user.vo.AppUpgradesVersionVO;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName AppInitFacadeImpl
 * @Description
 * @Author hanjunjun
 * @Date 2024/7/13 10:40
 */
@ServiceBehavior
@Slf4j
public class AppInitFacadeImpl implements IAppInitFacade {

    @Resource
    private IApolloService apolloService;

    @ZZMethod(value = "getAppInitConfig", desc = "app初始化配置")
    @Override
    public ZZOpenScfBaseResult<AppInitConfVO> getAppInitConfig(@ZZHeader ServiceHeaderEntity header,
        @NotNull @ZZParam("deviceToken") String deviceTokenStr,
        @NotNull @ZZParam("osVersion") String osVersion,
        @ZZParam("imei") String imei,
        @ZZParam("idfa") String idfa) {
        //端
        String terminal = ServiceHeaderUtil.parseTvalue(header);
        //版本
        String version = ServiceHeaderUtil.parseVsersion(header);
        //渠道ID
        String channelId = header.getChannelid() != null ? header.getChannelid().trim() : "";
        log.info("getAppInitConfig terminal={} version={} channelId={}", terminal, version, channelId);

//        String deviceToken = getDeviceToken(deviceTokenStr);

        AppInitConfigBO appInitConfig = apolloService.queryAppInitConfig();
        Map<String, AppInitConfigBO.AppUpdateConfigItem> appUpdateConfigMap = appInitConfig.getAppUpdateConfigMap();

        //返回基本配置信息
        AppInitConfVO config = new AppInitConfVO();
        config.setLoginByNameVer(apolloService.queryIosVirtualLoginVer());
        config.setServerTime(System.currentTimeMillis());
        //控制谷歌广告展示
        config.setCloseidfa(appInitConfig.getAppInitCloseIdfa());
        if (appUpdateConfigMap != null && appUpdateConfigMap.containsKey(terminal)) {
            AppInitConfigBO.AppUpdateConfigItem item = appUpdateConfigMap.get(terminal);
            config.setForceUpdateVersion(item.getVersion());
            config.setForceUpdateWarningTip(item.getTips());
            config.setForceUpdateJumpUrl(item.getAppStoreUrl());
        }

        return ZZOpenScfBaseResult.buildSucc(config);
    }

    @ZZMethod(value = "getAppInitConfigAndroid", desc = "安卓初始化")
    @Override
    public ZZOpenScfBaseResult<List<AppInitConfAndroidVO>> getAppInitConfigAndroid(@ZZHeader ServiceHeaderEntity header) {
        List<AppInitConfAndroidBO> appInitConfAndroidList = apolloService.queryIosVirtualLoginVerAndroid();
        List<AppInitConfAndroidVO> vos = new ArrayList<>();

        for (AppInitConfAndroidBO bo : appInitConfAndroidList) {
            AppInitConfAndroidVO vo = new AppInitConfAndroidVO();
            vo.setBrand(bo.getBrand());
            vo.setVersion(bo.getVersion());
            vos.add(vo);
        }
        return ZZOpenScfBaseResult.buildSucc(vos);
    }

    @ZZMethod(value = "getAppUpdateConfig", desc = "app更新")
    @Override
    public ZZOpenScfBaseResult<AppUpgradesVersionVO> getAppUpdateConfig(@ZZHeader ServiceHeaderEntity header) {
        Boolean open = apolloService.appUpgradesSwitch();
        AppUpgradesVersionVO vo = new AppUpgradesVersionVO();
        if(open){
            AppUpgradesVersionBO bo = apolloService.queryAppUpgradesVersion();
            if (Objects.nonNull(bo)){
                vo.setUpdateType(bo.getUpdateType());
                vo.setDescription(bo.getDescription());
                vo.setVersionNumber(bo.getVersionNumber());
                vo.setUrl(bo.getUrl());
            }
        }else{
            vo.setUpdateType(NumConsts.STRING_ZERO);
        }

        return ZZOpenScfBaseResult.buildSucc(vo);
    }

    /*private String getDeviceToken(String deviceTokenStr) {
        String deviceToken;
        if (StringUtils.isNotBlank(deviceTokenStr)) {
            deviceToken = deviceTokenStr.replaceAll("<|>|\\s", "");
        } else {
            deviceToken = "";
        }
        return deviceToken;
    }*/
}
